//
//  SliderView.m
//  UI12-QQMusic-xib
//
//  Created by 韩旭 on 16/8/20.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "SliderView.h"
#import "Masonry.h"
@interface SliderView ()
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, strong) void (^changeSlider)(CGFloat value);
@property (nonatomic, strong) void (^changeSliderValue)(UISlider *slider);

@end

@implementation SliderView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.musicCurrentProgressLable = [[UILabel alloc] init];
        [self addSubview:self.musicCurrentProgressLable];
        self.musicCurrentProgressLable.text = @"00:00";
        self.musicCurrentProgressLable.textAlignment = NSTextAlignmentCenter;
        self.musicCurrentProgressLable.font = [UIFont systemFontOfSize:13];
        self.musicCurrentProgressLable.textColor = [UIColor whiteColor];
        self.sider = [[UISlider alloc] init];
        [self addSubview:self.sider];
        [self.sider addTarget:self action:@selector(changeValue:) forControlEvents:UIControlEventValueChanged];
        self.musicOverallProgress = [[UILabel alloc] init];
        [self addSubview:self.musicOverallProgress];
        self.musicOverallProgress.text = @"--:--";
        self.musicOverallProgress.textAlignment = NSTextAlignmentCenter;
        self.musicOverallProgress.font = [UIFont systemFontOfSize:13];
        self.musicOverallProgress.textColor = [UIColor whiteColor];
        
        [self.musicCurrentProgressLable mas_makeConstraints:^(MASConstraintMaker *make) {
            // Y的坐标与View的中心点的Y相同
            make.centerY.mas_equalTo(self.mas_centerY);
            // 固定大小
            make.size.mas_equalTo(CGSizeMake(60, 40));
            // 使其左坐标系与自定义view的左边距为5
            make.left.mas_equalTo(self.mas_left).with.offset(5);
        }];
        [self.musicOverallProgress mas_makeConstraints:^(MASConstraintMaker *make) {
            // Y的坐标与View的中心点的Y相同
            make.centerY.mas_equalTo(self.mas_centerY);
            // 固定大小
            make.size.mas_equalTo(CGSizeMake(60, 40));
            // 使其右坐标系与自定义view的右边距为5
            make.right.mas_equalTo(self.mas_right).with.offset(-5);
        }];
        [self.sider mas_makeConstraints:^(MASConstraintMaker *make) {
            // 设置其中心点与ws相同
            make.center.mas_equalTo(self.center);
            // 设置左边距与当前进度条的右边
            make.left.mas_equalTo(self.musicCurrentProgressLable.mas_right).with.offset(0);
            // 设置右边距与当前进度条的左边
            make.right.mas_equalTo(self.musicOverallProgress.mas_left).with.offset(0);
            // 设置高度为40
            make.height.mas_equalTo(40);
        }];

    }
    return self;
}

- (void)addPlayerTimer {
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.5f target:self selector:@selector(changeTimer) userInfo:nil repeats:YES];
   [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
    self.timer = timer;
}

- (void)changeTimer {
    self.changeSliderValue(self.sider);
}
- (void)currentValue:(CGFloat)currentValue {
    self.sider.value = currentValue;
    self.musicCurrentProgressLable.text = [self changeTimer:currentValue];
}

//将播放时长转化成字符串
- (NSString *)changeTimer:(CGFloat)time
{
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:time];
    NSDateFormatter *former = [[NSDateFormatter alloc] init];
    if (time >= 3600) {
        [former setDateFormat:@"HH:mm:ss"];
    } else {
        [former setDateFormat:@"mm:ss"];
    }
    NSString *dateString = [former stringFromDate:date];
    return dateString;
}
- (void)changeValue:(id)btn {
    UISlider *slider = (UISlider *)btn;
    self.musicCurrentProgressLable.text = [self changeTimer:slider.value];
}
- (void)changeSliderValue:(void (^)(CGFloat))changeValue {
    self.changeSlider = changeValue;
}
- (void)playerSliderValue:(void (^)(UISlider *))changeSlider {
    self.changeSliderValue = changeSlider;
}
- (void)sliderThumb:(NSString *)thumbImage maximumTrackImage:(NSString *)maximumTrackImageName minimumTrackImage:(NSString *)minimumTrackImageName {
    [self.sider setThumbImage:[UIImage imageNamed:thumbImage] forState:UIControlStateNormal];
    [self.sider setMaximumTrackImage:[UIImage imageNamed:maximumTrackImageName] forState:UIControlStateNormal];
    [self.sider setMinimumTrackImage:[UIImage imageNamed:minimumTrackImageName] forState:UIControlStateNormal];
}
- (void)musicOverallValue:(CGFloat)overallValue {
    self.sider.maximumValue = overallValue;
    self.musicCurrentProgressLable.text = [self changeTimer:overallValue];
}


- (void)removePlayerTimer
{
    [self.timer invalidate];
    self.timer = nil;
}
- (void)playStatusClear
{
    // 设置Slider的Value值为0
    [self.sider setValue:0.0];
    // 设置播放器的时间为0
    [self.musicCurrentProgressLable setText:@"00:00"];
    // 移除定时器
    [self removePlayerTimer];
}

@end
